package com.feilong.edgeAI.speech.util;

import android.content.Context;
import android.media.AudioFormat;
import android.media.AudioRecord;
import android.media.MediaRecorder;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Looper;
import android.os.Message;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;


public class MsgProcess {

    private boolean testAduio = true;

    private AudioHandler mAudioReadHandler;


    public MsgProcess(Context context) {

        HandlerThread audioThread = new HandlerThread("audio");
        audioThread.start();
        mAudioReadHandler = new AudioHandler(audioThread.getLooper());
    }

    public interface OnRecordLinstener{
        void onRecording(byte[] data);
    }
    private OnRecordLinstener onRecordLinstener;
    public void setOnRecordLinstener(OnRecordLinstener onRecordLinstener){
        this.onRecordLinstener = onRecordLinstener;
    }
    public void stop() {
        mAudioReadHandler.sendEmptyMessage(AudioHandler.AUDIO_STOP);
    }

    public void startReadAudio() {
        mAudioReadHandler.sendEmptyMessage(AudioHandler.AUDIO_START);
    }


    class AudioHandler extends Handler {

        public static final int AUDIO_START = 0;
        public static final int AUDIO_READ = 1;
        public static final int AUDIO_STOP = 3;

        private AudioRecord mAudioRecord;
        private boolean mAudioStart;
        private FileOutputStream fileOutputStream;


        public AudioHandler(Looper looper) {
            super(looper);
        }


        int readcount = 0;
        int minBufferSize;

        @Override
        public void handleMessage( Message msg) {
            super.handleMessage(msg);


            switch (msg.what) {
                case AUDIO_START:
                    try {
                        if (mAudioStart) {
                            break;
                        }
                        if (testAduio) {
                            try {
                                fileOutputStream = new FileOutputStream("/sdcard/remix.pcm");
                            } catch (FileNotFoundException e) {
                                e.printStackTrace();
                            }
                        }
                        int sample = 16000;

                        minBufferSize = AudioRecord.getMinBufferSize(sample, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT);
                        mAudioRecord = new AudioRecord(MediaRecorder.AudioSource.MIC, sample, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT, minBufferSize);

                        mAudioRecord.startRecording();
                        mAudioStart = true;
                        mAudioReadHandler.sendEmptyMessage(AUDIO_READ);

                    } catch (Exception e) {
                        e.printStackTrace();
                        mAudioStart = false;
                    }

                    break;
                case AUDIO_READ:
                    if (mAudioStart) {
                        byte[] data = new byte[minBufferSize];
                        int len = 0;
                        try {
                            len = mAudioRecord.read(data, 0, data.length);
                            readcount++;
                            byte[] soundwrite = new byte[len];
                            System.arraycopy(data, 0, soundwrite, 0, len);
                            if(null!=onRecordLinstener){
                                onRecordLinstener.onRecording(data);
                            }
                            // if(readcount < 500)
                            mAudioReadHandler.sendEmptyMessage(AUDIO_READ);
                            // else
                            //   soundFeature.commitAudio();

                        } catch (Exception e) {
                            e.printStackTrace();
                            mAudioStart = false;
                        }

                        if (testAduio) {
//                            try {
////                                fileOutputStream.write(data, 0, len);
//                            } catch (IOException e) {
//                                e.printStackTrace();
//                            }
                        }
                    }

                    break;
                case AUDIO_STOP:
                    try {
                        if (mAudioStart) {
                            mAudioRecord.stop();
                            mAudioStart = false;
                        }

                    } catch (Exception e) {
                        e.printStackTrace();
                        mAudioStart = false;
                    }
                    break;
            }

        }
    }
}
